﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class ReadRecodInfo : BasePage
    {
        public Guid CustomerID
        {
            get
            {
                if (hiddenCustomerID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenCustomerID.Value);
            }
            set
            {
                hiddenCustomerID.Value = value.ToString();
            }
        }
        public String SerialNum
        {
            get
            {
                if (hiddenSerialNum.Value == string.Empty) return String.Empty;
                else return hiddenSerialNum.Value;
            }
            set
            {
                hiddenSerialNum.Value = value.ToString();
            }
        }
        public Guid InstrumentID
        {
            get
            {
                if (HiddenInstrumentID.Value == string.Empty) return Guid.Empty;
                else return new Guid(HiddenInstrumentID.Value);
            }
            set
            {
                HiddenInstrumentID.Value = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (Request.QueryString["CustomerID"] != null && Request.QueryString["SerialNum"] != null && Request.QueryString["InstrumentID"] != null)
                {
                    InstrumentID = new Guid(Request.QueryString["InstrumentID"]);
                    CustomerID = new Guid(Request.QueryString["CustomerID"]);
                    SerialNum = Request.QueryString["SerialNum"];

                    List<WsmInstrumentRecordEntity> recordList = new SystemBll().GetAll<WsmInstrumentRecordEntity>(" and SerialNum = '" + SerialNum + "' and CustomerID='" + CustomerID + "'");
                    if (recordList != null && recordList.Count > 0)
                    {
                        ReadNumType.Enabled = false;
                        ReadNumType.SelectedValue = recordList[0].ReadNumType.ToString();
                    }
                    DataTable dtnearlyRecord = new WsmInstrumentRecordBll().GetInstrumentRecordByConfirmParms(SerialNum, InstrumentID, CustomerID);
                    if (dtnearlyRecord != null && dtnearlyRecord.Rows.Count > 0)
                    {
                        hiddenLastRecordNum.Text = dtnearlyRecord.Rows[0]["TotalNum"].ToString();
                        hiddenLastRecordDate.Text = dtnearlyRecord.Rows[0]["RecordDate"].ToString();
                    }
                    else
                    {
                        hiddenLastRecordNum.Text = "0";
                        hiddenLastRecordDate.Text = DateTime.Now.ToString();
                    }
                }
            }
        }
        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (ReadNumDate.SelectedDate == null) {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请填写登记日期');", true);
                return;
            }
            if (ReadNumDate.SelectedDate.Value.Date.CompareTo(DateTime.Now.Date)>0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('登记日期不能大于当前日期');", true);
                return;
            }
            WsmInstrumentRecordEntity entity = new WsmInstrumentRecordEntity();
            entity.RecordID = Guid.NewGuid();
            entity.Recorder = CurrentUserID; 
            entity.RecordDate = ReadNumDate.SelectedDate.Value;
            entity.InstrumentID = InstrumentID;
            entity.CreatedBy = CurrentUserID;
            entity.CreatedOn = DateTime.Now;
            entity.ReadNumType = System.Convert.ToInt32(ReadNumType.SelectedValue);
            entity.Deleted = false;
            entity.CustomerID = CustomerID;
            entity.SerialNum = SerialNum;

            if (ReadNumType.SelectedValue == "1") 
            {
                if (System.Convert.ToDecimal(ReadNum.Value) <= System.Convert.ToDecimal(hiddenLastRecordNum.Text))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "saveerror", "alert('计数总数不能小于上次总数');", true);
                    return;
                }
                entity.TotalNum = System.Convert.ToDecimal(ReadNum.Value);
                if (hiddenLastRecordNum.Text != "0")
                {
                    decimal lastTotal = System.Convert.ToDecimal(hiddenLastRecordNum.Text);
                    DateTime lastRecordDate = System.Convert.ToDateTime(hiddenLastRecordDate.Text);
                    int interval = entity.RecordDate.Date.Subtract(lastRecordDate.Date).Days;
                    decimal intevalRecord = System.Convert.ToDecimal(ReadNum.Value) - lastTotal;
                    decimal averRecord = intevalRecord / interval;
                    entity.DailyNum = averRecord;
                }
                else {
                    entity.DailyNum = System.Convert.ToDecimal(ReadNum.Value);
                }
            }                
            if (ReadNumType.SelectedValue == "2") {
                entity.DailyNum = System.Convert.ToDecimal(ReadNumAverageDaily.Value);
            }
            new SystemBll().Insert(entity);

            this.lnkbtnSaveAndClose.Visible = false;
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
        }              
    }
}
